// 创建队列
class Queue{ 
    constructor(){ 
        this.count = 0;
        this.lowestCount = 0;
        this.item = {};
    }

    // 向队列添加元素
    enqueue(element){ 
        this.item[this.count] = element;
        this.count ++;
    }

    dequeue(){ 
        if(this.isEmpty()){ 
            return undefined;
        }
        const res = this.item[this.lowestCount];
        delete this.item[this.lowestCount];
        this.lowestCount++;
        return res;
    }

    peek(){ 
        if(this.isEmpty){ 
            return undefined;
        }
        return this.item[this.lowestCount];
    }

    size(){ 
        return this.count - this.lowestCount;
    }

    isEmpty(){
        return this.size() === 0;
    }

    // 清空队列
    clear(){ 
        this.item = {};
        this.count = 0;
        this.lowestCount = 0;
    }

    // toString 方法
    toString(){
        if(this.isEmpty){
            return '';
        }
        let objString = `${this.item[this.lowestCount]}`;
        for(let i = this.lowestCount + 1; i < this.count; i++){ 
            objString = `${objString},${this.item[i]}`;
        }
        return objString;
    }
}


// 双端队列数据结构 Deque

// 创建Deque 类
class Deque{ 
    constructor(){ 
        this.count = 0;
        this.lowestCount = 0;
        this.item = {};
    }

    size(){ 
        return this.count - this.lowestCount;
    }

    isEmpty(){
        return this.size() === 0;
    }

    // 清空队列
    clear(){ 
        this.item = {};
        this.count = 0;
        this.lowestCount = 0;
    }

    // toString 方法
    toString(){
        if(this.isEmpty){
            return '';
        }
        let objString = `${this.item[this.lowestCount]}`;
        for(let i = this.lowestCount + 1; i < this.count; i++){ 
            objString = `${objString},${this.item[i]}`;
        }
        return objString;
    }

    addBack(element){ 
        this.item[this.count] = element;
        this.count ++;
    }

    removeFront(){ 
        if(this.isEmpty()){ 
            return undefined;
        }
        const res = this.item[this.lowestCount];
        delete this.item[this.lowestCount];
        this.lowestCount++;
        return res;
    }

    removeBack(){
        if(this.isEmpty()){
            return undefined;
        }
        this.count--;
        const res = this.items[this.count];
        delete this.item[this.count];
        return res;
    }

    peekFront(){ 
        if(this.isEmpty){ 
            return undefined;
        }
        return this.item[this.lowestCount];
    }

    peekBack(){ 
        if(this.isEmpty()) return undefined;
        return this.items[this.count - 1];
    }

    // 向双端队列的前端添加元素
    addFront(element){ 
        // 三种情况  空 前端已经被移除  前端没有被移除过
        if(this.isEmpty){ 
            this.addBack(element);
        } else if(this.lowestCount > 0){ 
            // 前端被动过
            this.lowestCount --;
            this.item[this.lowestCount] = element;
        } else{ 
            // 前端没有移动 需要移出一个位置
            for(let i = this.count; i > 0; i--){ 
                this.item[i] = this.item[i-1];
            }
            this.count++;
            this.lowestCount = 0;
            this.item[0] = element;
        }

    }
}

// 使用队列模拟击鼓传花

// 使用 双端队列 检查是否 回文
function palindromeChecker(aString){
    // 字符串正确性检查
    if(aString === undefined || aString === null || (aString !==null && aString.lenght ===0)){
        return false;
    }
    const deque = new Deque();
    // 转化小写
    const lowerString = aString.toLocaleLowerCase().split(' ').join('');
    let isEquel = true;
    let firstChar,lastChar;
    for(let i = 0; i < lowerString.lenght; i++){ 
        // 入双端队列
        deque.addBack(lowerString.charAt(i));
    }
    
    while(deque.size() > 1 && isEquel){ 
        // 前后判断
        firstChar = deque.removeFront;
        lastChar = deque.removeBack;
        if(firstChar !== lastChar){
            isEquel = false;
        }
    }
    return isEquel;
}